<?php
class NX_File{
	//创建 多级目录
	public static function makeDirs($d,$m=null){
		if(is_dir($d)) return true;
		if($m == null) $m = '0777';
		$df = '';
		$d = str_ireplace('\\','/',$d);
		if(substr($d,0,1)=='/'){
			$d = substr($d,1);
			$df ='/';
		}
		$dir = explode('/',$d);
		foreach ($dir as $k=>$v){
			$df.=$v.'/';
			if(!is_dir($df)){
				@mkdir($df,$m);
			}
		}
		if(is_dir($df)) return true;
		else return false;
	}
	// 读取文件
	public static function readFile($fp)
	{
		if (file_exists($fp) && ($fd = @fopen($fp,'rb')) ) {
			$contents = '';
			while (!feof($fd)) {
				$contents .= fread($fd, 8192);
			}
			fclose($fd);
			return $contents;
		} else {
			return false;
		}
	}
	//保存文件
	public static function saveFile($fp,$fd,$wt='w'){
		$r=self::makeDirs(dirname($fp));
		for ($i=1;$i<3;$i++){
			$wfp = fopen($fp,$wt);
			flock($wfp,LOCK_EX);
			@fwrite($wfp,$fd);
			flock($wfp,LOCK_UN);
		}

		if(file_exists($fp)){
			return true;
		}else{
			return false;
		}
	}
	//读目录里的文件
	public static function getFilesInDir($dir,$odk='time'){
		clearstatcache();
		$handle=opendir($dir);
		if($odk=='time'){
			while($file=readdir($handle)){
				$dfp = $dir.'/'.$file;
				if(is_dir($dfp)){
					$list['d'][$file]=0;
				}elseif(is_file($dfp)){
					$list['f'][$file]=filectime($dfp);
				}
			}
		}elseif ($odk=='size'){
			while($file=readdir($handle)){
				$dfp = $dir.'/'.$file;
				if(is_dir($dfp)){
					$list['d'][$file]=0;
				}elseif(is_file($dfp)){
					$list['f'][$file]=filesize($dfp);
				}
			}
		}
		closedir($handle);
		asort($list['f']);
		return $list;
	}
	//文件 的真实类型
	public static function getFileTypeExt($fp){
		$file = fopen($fp,"rb");
		$bin  = fread($file,2); //只读2字节
		fclose($file);
		$strInfo  = @unpack("c2chars",$bin);
		$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
		$fileType = '';
		switch ($typeCode)
		{
			case -48:
				$fileType = 'doc';
				break;
			case 7790:
				$fileType = 'exe';
				break;
			case 7784:
				$fileType = 'midi';
				break;
			case 8297:
				$fileType = 'rar';
				break;
			case 255216:
				$fileType = 'jpg';
				break;
			case 7173:
				$fileType = 'gif';
				break;
			case 6677:
				$fileType = 'bmp';
				break;
			case 13780:
				$fileType = 'png';
				break;
			default:
				$fileType = 'unknown';
		}
		return $fileType;
	}
	//文件 上传
	public static function upload($fn,$sfp){
		if(!self::makeDirs(dirname($sfp))){
			return false;
		}
		if (function_exists('move_uploaded_file') ){
			@move_uploaded_file($fn,$sfp);
			//上传方式1 使用 move_uploaded_file
		}elseif(function_exists('copy')){
			@copy($fn,$sfp);
			//上传方式2 使用 copy
		}elseif(@is_readable($fn)){
			if ($fp = @fopen($fn,'rb')){
				@flock($fp,2);
				$filedata = @fread($fp,@filesize($fn));
				@fclose($fp);
			}
			//以上if 上传的文件 是否可读
			if($fp = @fopen($sfp,'wb')){
				@flock($fp,2);
				@fwrite($fp,$sfp);
				@fclose($fp);
			}
			//以上if 保存的位置 是否可写
			unset($fp);
			//上传方式3 使fwrite
		}
		if(file_exists($sfp)){
			return true;
		}
		else{
			return false;
		}
	}
	// 指定的随机文件名
	public static function getUserSetName($type){
		$tarr = array('YmdHis');
		if(!preg_match("/YmdHis_([\d]+)/i",$type) && !in_array($type,$tarr)){
			$type = 'YmdHis_16';
		}
		if($type == "YmdHis"){
			$sname=date($type,time());
		}elseif($n=intval(str_ireplace('YmdHis_','',$type))){
			$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
			$sname=date("YmdHis",time());
			$estr='';
			for($i=0;$i<$n;$i++){
				$estr.=substr($chars,(rand()%62),1);
			}
			$sname.="_".$estr;
			unset($chars,$n,$i,$estr);
		}
		return $sname;
	}
	//文件名后缀
	public static function getFileNameExt($filename){
		return trim(substr(strrchr($filename, '.'), 1));
	}

	//文件名 转为 存储路径
	public static function getFileCachePath($fn){
		$fp = '';
		$len = 128;			//每级Cache最多个目录数
		$fn = urlencode($fn);
		$fn_len = strlen($fn);
		$fp = ($fn_len % $len).'/';
		if($fn_len>$len){
			for($i=0;$i<$fn_len;$i=$i+$len){
				$fp.= substr($fn,$i,$len);
				$fp.='/';
			}
			$fp = substr($fp,0,-1);
		}
		else{
			$fp.=$fn;
		}
		return $fp;
	}
}